package site.xuxingcan.util;

import com.google.gson.Gson;
import site.xuxingcan.model.WXSyncModel;

import java.util.Collections;

/**
 * Created by xuxingcan on 2016/9/22.
 */
public class HandleUtil {
    public static Gson gson = new Gson();

    /**
     * 获取最新消息
     */
    public static void HandleMsg(String data) {
        if (null == data) {
            return;
        }
        WXSyncModel model = gson.fromJson(data, WXSyncModel.class);
        for (WXSyncModel.AddMsgListBean msg : model.getAddMsgList()) {
            switch (msg.getMsgType()) {
                case 1:
                    LogUtil.info("[*] 你有新的消息，请注意查收");
                    String content = msg.getContent();
//                    if (content.contains("<br/>")) {
//                        content = content.substring(content.indexOf("<br/>") + "<br/>".length());
//                    }
                    LogUtil.info("[*] 内容:" + content);
                    if (msg.getToUserName().equalsIgnoreCase("filehelper")) {
                        if (content.equalsIgnoreCase("帮助")) {
                            String s = "本消息来自机器人:\n输入“下注关键字+关键字”可设置下注关键字\n输入“好友公告+关键字”可设置好友公告\n输入“下注延时+数字”可设置下注延时\n输入“下注列表+\n消息1\n消息2”换行可设置下注列表";
                            WeiXinUtil.webwxsendmsg(s, "filehelper");
                        } else if (content.equalsIgnoreCase("设置")) {
                            String s = "本消息来自机器人:\n当前下注关键字：" + WeiXinUtil.XIAZHU_KEY + "\n当前下注延时：" + WeiXinUtil.XIAZHU_TIME + "\n当前下注列表：";
                            for (String t : WeiXinUtil.XIAZHU_LIST) {
                                s += "\n" + t;
                            }
                            WeiXinUtil.webwxsendmsg(s, "filehelper");
                        } else if (content.startsWith("下注关键字+")) {
                            WeiXinUtil.XIAZHU_KEY = content.substring("下注关键字+".length());
                            String s = "本消息来自机器人:\n设置成功\n当前下注关键字：" + WeiXinUtil.XIAZHU_KEY;
                            WeiXinUtil.webwxsendmsg(s, "filehelper");
                        } else if (content.startsWith("好友公告+")) {
                            WeiXinUtil.GONGGAO = content.substring("好友公告+".length());
                            String s = "本消息来自机器人:\n设置成功\n当前好友公告：" + WeiXinUtil.GONGGAO;
                            WeiXinUtil.webwxsendmsg(s, "filehelper");
                        } else if (content.startsWith("下注延时+")) {
                            try {
                                WeiXinUtil.XIAZHU_TIME = Integer.parseInt(content.substring("下注延时+".length()));
                                String s = "本消息来自机器人:\n设置成功\n当前下注延时：" + WeiXinUtil.XIAZHU_TIME;
                                WeiXinUtil.webwxsendmsg(s, "filehelper");
                            } catch (Exception e) {
                                e.printStackTrace();
                                String s = "本消息来自机器人:\n设置失败\n当前下注延时：" + WeiXinUtil.XIAZHU_TIME;
                                WeiXinUtil.webwxsendmsg(s, "filehelper");
                            }
                        } else if (content.startsWith("下注列表+")) {
                            try {
                                String[] strings = content.substring("下注列表+<br/>".length()).split("<br/>");
                                if (strings.length <= 0) {
                                    throw new Exception();
                                }
                                WeiXinUtil.XIAZHU_LIST.clear();
                                for (String s : strings) {
                                    WeiXinUtil.XIAZHU_LIST.add(s);
                                }
                                String s = "本消息来自机器人:\n设置成功\n当前下注列表：";
                                for (String t : WeiXinUtil.XIAZHU_LIST) {
                                    s += "\n" + t;
                                }
                                WeiXinUtil.webwxsendmsg(s, "filehelper");
                            } catch (Exception e) {
                                e.printStackTrace();
                                String s = "本消息来自机器人:\n设置失败\n当前下注列表：";
                                for (String t : WeiXinUtil.XIAZHU_LIST) {
                                    s += "\n" + t;
                                }
                                WeiXinUtil.webwxsendmsg(s, "filehelper");
                            }
                        }
                    } else {
                        if (content.equalsIgnoreCase("ID")) {
                            WeiXinUtil.webwxsendmsg(msg.getFromUserName(), msg.getFromUserName());
                        } else if (content.contains(WeiXinUtil.XIAZHU_KEY)) {
                            Collections.shuffle(WeiXinUtil.XIAZHU_LIST);
                            final String s = WeiXinUtil.XIAZHU_LIST.get(0);
                            final String from = msg.getFromUserName();
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    while (true) {
                                        try {
                                            Thread.sleep(WeiXinUtil.XIAZHU_TIME * 1000);
                                            String res = WeiXinUtil.webwxsendmsg(s, from);
                                            if (res != null && res.length() != 0) {
                                                break;
                                            }
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }
                            }).start();
                        }
                    }
                    break;
                case 37:
                    if (msg.getRecommendInfo() != null && msg.getRecommendInfo().getUserName() != null && msg.getRecommendInfo().getTicket() != null) {
                        WeiXinUtil.webAddUser(msg.getRecommendInfo().getUserName(), msg.getRecommendInfo().getTicket());
                        final String user = msg.getRecommendInfo().getUserName();
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                while (true) {
                                    try {
                                        Thread.sleep(1 * 1000);
                                        String res = WeiXinUtil.webwxsendmsg(WeiXinUtil.GONGGAO, user);
                                        if (res != null && res.length() != 0) {
                                            break;
                                        }
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }).start();
                    }
                    break;
            }
        }
    }
}
